#include "ModelNode.h"
#include "IRenderer.h"

#ifdef _DEBUG
#include <Windows.h>
#include <gl/GL.h>
#endif

ModelNode::ModelNode( Pointer<ModelInstance>& modelInstance ) : m_modelInstance(modelInstance)
{
}

HashedString ModelNode::Type() const
{
	return HashedString("ModelNode");
}

void ModelNode::Enable()
{
	for (MeshInstance::Vector_t::iterator it = m_modelInstance->getMeshInstances().begin(), end = m_modelInstance->getMeshInstances().end(); it != end; ++it)
	{
		Material& material = it->getMesh()->getMaterial();
		
		Renderer->SetMaterial(material);
	
		Pointer<IVertexContainer> container;
		if (material.m_shader == HashedString("Skeleton"))
		{
			container = new IVertexContainer(it->getMesh()->getVertexContainer()->getVertexDeclaration());
			for (unsigned int i = 0; i < it->getMesh()->getVertexContainer()->AttributeIndex().size(); ++i)
				container->AttributeIndex().push_back(it->getMesh()->getVertexContainer()->AttributeIndex()[i]);
			container->resize(it->getMesh()->getVertexContainer()->getVerticeCount());

			// Renderer->UnsetShader();
		} else
			container = it->getMesh()->getVertexContainer();
		//Shader& shader = Renderer->getShader(material.m_shader);

		//UniformData data;
		//for (IUniformInstruction::Map_t::iterator uiit = shader.m_uniformInstructions.begin(); uiit != shader.m_uniformInstructions.end(); ++uiit)
		//{
		//	data = (*uiit->second)(m_modelInstance);
		//	Renderer->SetUniform(material.m_shader, uiit->first, data.count, (float*)data.data, data.transpose);
		//}
		
		// SetTextures();
		Renderer->Render(*container);
		Renderer->UnsetMaterial();

//#ifdef _DEBUG
//		unsigned char* vertexptr = (unsigned char*)(it->getMesh()->getVertexContainer()->getVertexPointer());
//		unsigned char* normalptr = (unsigned char*)(it->getMesh()->getVertexContainer()->getVertexPointer());
//		unsigned int normaloffset;
//		unsigned int vertexoffset;
//		unsigned int vertexsize = it->getMesh()->getVertexContainer()->getVertexDeclaration().m_vertexSize;
//
//		for (VertexType::Vector_t::const_iterator param = it->getMesh()->getVertexContainer()->getVertexDeclaration().m_parameters.begin(); param != it->getMesh()->getVertexContainer()->getVertexDeclaration().m_parameters.end(); ++param)
//		{
//			if (param->m_parameter == enumNormal)
//			{
//				normaloffset = param->m_offset;
//			}
//			if (param->m_parameter == enumPosition)
//			{
//				vertexoffset = param->m_offset;
//			}
//		}
//		vertexptr += vertexoffset;
//		normalptr += normaloffset;
//
//		glBegin(GL_LINES);
//		for (int i = 0; i < it->getMesh()->getVertexContainer()->getVerticeCount(); ++i)
//		{
//			float* v = (float*)vertexptr;
//			float* n = (float*)normalptr;
//			glVertex3f(v[0], v[1], v[2]);
//			glVertex3f(v[0] + n[0], v[1] + n[1], v[2] + n[2]);
//			vertexptr += vertexsize;
//			normalptr += vertexsize;
//		}
//		glEnd();
//#endif
	}
}

void ModelNode::Disable()
{
}

Pointer<ModelInstance>& ModelNode::getModelInstance()
{
	return m_modelInstance;
}